﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Controls;
using Merise_designer.Objets;
using System.Windows;

namespace Merise_designer.Methodes
{
    public class Associations
    {
        private List<CL_Association> mAssociation;
        private Donnees.CL_Associations dataAsso = new Donnees.CL_Associations();
        Methodes.ObjetsCommun controller;

        public Associations(Methodes.ObjetsCommun contructor, Grid grille)
        {
            this.controller = contructor;
            this.mAssociation = new List<CL_Association>();
        }
        public void load(Grid grille)
        {
            String jeu = "monjeu";
            Donnees.CL_Associations AssoData = new Donnees.CL_Associations();
            System.Data.DataSet associations = new System.Data.DataSet();

            associations = AssoData.selectAll(jeu);

            foreach (System.Data.DataRow theRow in associations.Tables[jeu].Rows)
            {
                //Convertion pour la position
                PointConverter pConverter = new PointConverter();
                Point position = new Point();
                string string1 = (string)theRow["POSITION_ASSO"];
                position = (Point)pConverter.ConvertFromString(string1.Replace(";", ","));

                //Créé l'association
                this.AddLoad(grille, (string)theRow["ID_ASSO"], (string)theRow["NOM_ASSO"], (string)theRow["CODE_ASSO"], Convert.ToDouble(theRow["HAUT_ASSO"]), Convert.ToDouble(theRow["LARG_ASSO"]), position);
            }
        }
        public void Add(Grid grille, Point position)
        {
            this.mAssociation.Add(new CL_Association(controller, grille, CL_Variables.addNameAuto(), position));
            //ajoute dans la base
            if (Mode.mode_save == Mode.MODE_SAVE_AUTO)
            {
                dataAsso.add(mAssociation.Last());
            }
        }
        public void AddLoad(Grid grille, string id, string nom, string codeNom, double hauteur, double largeur, Point position)
        {
            this.mAssociation.Add(new CL_Association(controller, grille, id, nom, codeNom, hauteur, largeur, position));
        }
        public void Del(Grid grille, string name)
        {
            Donnees.CL_DictionnaireDeDonnees dataDico = new Donnees.CL_DictionnaireDeDonnees();
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    //Supprime de la base
                    if (Mode.mode_save == Mode.MODE_SAVE_AUTO)
                    {
                        //dataDico.deleteAllFromAssociation(asso);
                        this.dataAsso.delete(asso.Id);
                    }

                    this.controller.getContructorDico().deleteAllFromAssociation(asso);
                    asso.Supprimer(grille);
                    mAssociation.Remove(asso);

                    return;
                }
            }
        }
        public void edit(string name)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    asso.edition(null, null);
                    return;
                }
            }

        }
        public int id(string name)
        {
            for (int i = 0; i < mAssociation.Count; i++)
            {
                if (mAssociation[i].Id == name)
                {
                    return i;
                }
            }
            return -1;
        }
        public double posLeft(string name)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    return asso.posLeft;
                }
            }
            return -1;
        }
        public double posTop(string name)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    return asso.posTop;
                }
            }
            return -1;
        }
        public void move(string name, Point from, Point to)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    asso.move(from, to);
                }
            }
        }
        public void getFocus(Grid grille, string name)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    asso.getFocus(grille);
                }
            }
        }
        public void lostFocus(Grid grille, string name)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    asso.lostFocus(grille);
                }
            }
        }
        public CL_Association getAssociation(string name)
        {
            CL_Association result = null;

            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == name)
                {
                    result = asso;
                    return result;
                }
            }
            return result;
        }
        public CL_Association getLastAssociation()
        {
            return this.mAssociation[this.mAssociation.Count - 1];
        }
        public List<CL_Association> getAllAssociations()
        {
            return this.mAssociation;
        }
        public void movePoignees(string nameAsso, Point newCursor)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == nameAsso)
                {
                    asso.movePoignees(newCursor);
                    return;
                }
            }
        }
        public int getIDpoignee(string nameAsso, Point cursor)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == nameAsso)
                {
                    return asso.getIDofPoigneeSelect(cursor);
                }
            }
            return -1;

        }
        public void afficheContenu(string monJeu)
        {
            foreach (CL_Association asso in mAssociation)
            {
                asso.afficheContenu(this.controller.getContructorDico().selectAllFromAssociation(asso, monJeu));
            }
        }
        public void afficheContenuMLD(string monJeu, Tuple<Entites, Lignes, Associations> m_objets)
        {
            foreach (CL_Association ass in mAssociation)
            {
                afficheContenuMLDof(ass, m_objets.Item2, m_objets.Item1);
            }
        }
        public List<CL_DictionnaireDeDonnees> afficheContenuMLDof(CL_Association ass, Lignes m_Lines,Entites m_Entites)
        {
            List<CL_DictionnaireDeDonnees> donneesBrut = new List<CL_DictionnaireDeDonnees>();
            List<CL_Line> myLines = m_Lines.getAllLines();
            List<CL_Entite> entityWithAssoN = new List<CL_Entite>();
            List<String> entityWithAssoNCardinalite = new List<String>();

            entityWithAssoN.Clear();
            entityWithAssoNCardinalite.Clear();
            
            //Récupère les entités connectés de type N
            foreach (CL_Line line in myLines)
            {
                //Pour chaque lignes connectés dont la cardinalité finit par "N"
                if (line.IsConnectedToAssociation(ass) & line.Cardinalite.EndsWith("N"))
                {
                    //Récupère la liste des entités concernées par une asso type N
                    entityWithAssoN.Add(m_Entites.getEntite(line.IdEntite));
                    entityWithAssoNCardinalite.Add(line.Cardinalite);
                }
            }

            donneesBrut = this.controller.getContructorDico().selectAllFromAssociation(ass, "monJeu");

            //si plus de deux entités avec asso N (donc si association type N->N) => Inserrer la clé étrangère
            if (entityWithAssoN.Count > 1)
            {
                //inserre les clé étrangère
                for (int c = 0; c < entityWithAssoN.Count; c++)
                {
                    //Inserre la clé étrangère
                    List<CL_DictionnaireDeDonnees> donneesKey = this.controller.getContructorDico().selectAllFromEntite(entityWithAssoN[c], "monJeu");
                    for (int i = 0; i < donneesKey.Count; i++)
                    {
                        //Récupère les clés primaires da la table et les place en clé étrangère
                        if (donneesKey[i].pk == true)//.Tables[0].Rows[i]["PK"].ToString() == "True")
                        {
                            String strO = this.controller.getContructorDico().getStringO(donneesKey[i].O);
                            CL_DictionnaireDeDonnees d = new CL_DictionnaireDeDonnees(donneesKey[i].id, donneesKey[i].nom, donneesKey[i].code, donneesKey[i].idType, donneesKey[i].stringType, donneesKey[i].idAsso, donneesKey[i].idEntite, donneesKey[i].pk, donneesKey[i].O, strO, true);
                            //d.O = entityWithAssoNCardinalite[c].StartsWith("1") ? true : false; //Convertions du champs obligatoire en fonction des cardinalités
                            //d.stringO = this.controller.getContructorDico().getStringO(d.O);
                            donneesBrut.Add(d);
                        }
                    }
                }

            }

            if (Mode.mode == Mode.MODE_MLD) ass.afficheContenu(donneesBrut);
            return donneesBrut;
        }
        public void saveUpdate(string nameAsso, Boolean force)
        {
            foreach (CL_Association asso in mAssociation)
            {
                if (asso.Id == nameAsso)
                {
                    if (Mode.mode_save == Mode.MODE_SAVE_AUTO || force)
                    {
                        dataAsso.update(asso);
                    }
                    return;
                }
            }
        }
        public void switchMode()
        {
            foreach (CL_Association asso in mAssociation)
            {
                asso.switchMode();
            }

        }
    }
}
